package com.vincent.kdp.ui.vip;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.vincent.baseapp.util.CommonUtil;
import com.vincent.kdp.R;
import com.vincent.kdp.bean.RegisterVo;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.ui.bean.response.BaseOutput;
import com.vincent.kdp.ui.bean.response.RegisterOutPut;
import com.vincent.kdp.ui.bean.response.RegisterVipOutput;
import com.vincent.kdp.ui.vip.repository.GetVipLevelRepository;
import com.vincent.kdp.ui.vip.repository.RegisterRepository;
import com.vincent.kdp.util.RegexUtils;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends Activity {


    @Bind(R.id.back_img)
    ImageView mBackImg;
    @Bind(R.id.toolbar_center_tv)
    TextView mToolbarCenterTv;

    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.et_sex)
    TextView etSex;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_vip)
    TextView etVip;
    @Bind(R.id.et_card)
    EditText etCard;
    @Bind(R.id.et_address)
    EditText etAddress;
    @Bind(R.id.et_mail)
    EditText etMail;
    @Bind(R.id.et_birth)
    TextView etBirth;
    @Bind(R.id.bt_comfirm)
    Button confirmBtn;


    //存储VIP
    private ArrayList<String> list=new ArrayList<>();
    private ArrayList<String> listId=new ArrayList<>();
    //会员卡的ID
    private String id;

    private RegisterRepository registerRepository=new RegisterRepository();
    private GetVipLevelRepository getVipLevelRepository=new GetVipLevelRepository();

    private Activity  thisActivity;

    int mYear, mMonth, mDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        thisActivity=this;
        ButterKnife.bind(this);
        mToolbarCenterTv.setText("注册");
        initData();
        etBirth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCreateDialog();
            }
        });
        etSex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectSexDialog();
            }
        });

        etVip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showVipDialog();
            }
        });
    }

    private void showVipDialog() {
        final String[] vip=new String[list.size()];
        for(int x=0;x<list.size();x++){
            vip[x]=list.get(x);
        }
        AlertDialog.Builder vipDialog=new AlertDialog.Builder(this);
        vipDialog.setTitle("请选择等级");
        vipDialog.setSingleChoiceItems(vip, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                etVip.setText(vip[which]);
                id=listId.get(which);
                dialog.dismiss();
            }
        });
        vipDialog.create().show();
    }

    private void initData() {
        getVipLevelRepository.reqRegister(new
                RequestCallBack<RegisterVipOutput>() {
                    @Override
                    public void onSuccess(RegisterVipOutput object) {
                        for(int x=0;x<object.getData().size();x++){
                            list.add(object.getData().get(x).getName());
                            listId.add(object.getData().get(x).getId());
                        }
                    }
                    @Override
                    public void onFalse(String msg) {
                        CommonUtil.showToast(thisActivity,msg);
                    }
                });
    }

    private void selectSexDialog() {
        final String[] sex={"男","女"};
        AlertDialog.Builder dialog=new AlertDialog.Builder(this);
        dialog.setTitle("请选择性别");
        dialog.setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                etSex.setText(sex[which]);
                dialog.dismiss();
            }
        });
        dialog.create().show();
    }


    @OnClick({R.id.back_img, R.id.bt_comfirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_img:
                actFinish();
                break;
            case R.id.bt_comfirm:
                register();
                this.finish();
                break;
        }
    }

    protected void onCreateDialog() {
        thisActivity.runOnUiThread(new Runnable() {
            public void run() {
                Calendar calendar=Calendar.getInstance();
                mYear=calendar.get(Calendar.YEAR);
                mMonth=calendar.get(Calendar.MONTH);
                mDay=calendar.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog dialog=new DatePickerDialog(thisActivity, mdateListener, mYear, mMonth, mDay);
                dialog.show();
            }

        });
    }

    private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String date=new StringBuffer().append(year).append("-").
                    append(monthOfYear + 1).append("-").
                    append(dayOfMonth).append("").toString();
            etBirth.setText(date);
        }
    };

    private String sexId;
    private void register(){
        String name=RegexUtils.trimNull(etName.getText());
        String sex=RegexUtils.trimNull(etSex.getText());
        String phone=RegexUtils.trimNull(etPhone.getText());
        String birth=RegexUtils.trimNull(etBirth.getText());
        String card=RegexUtils.trimNull(etCard.getText());
        String address=RegexUtils.trimNull(etAddress.getText());
        String mail=RegexUtils.trimNull(etMail.getText());
        String vip=RegexUtils.trimNull(etVip.getText());

        if (name.isEmpty()) {
            CommonUtil.showToast(this,"姓名不能为空");
            return;
        }

        if(sex.equals("男")){
            sexId="0";
        }else if(sex.equals("女")){
            sexId="1";
        }

        if (phone.isEmpty()) {
            CommonUtil.showToast(this,"请输入手机号码！");
            return;
        }
        if(!RegexUtils.isMobileNO(phone)){
            CommonUtil.showToast(this,"手机号码格式不正确！");
            return;
        }
        if (birth.isEmpty()) {
            CommonUtil.showToast(this,"请输入生日！");
            return;
        }
        if (vip.isEmpty()) {
            CommonUtil.showToast(this,"请选择VIP等级！");
            return;
        }

        if(card.length()>0
                &&!RegexUtils.isPostCard(card)){
            CommonUtil.showToast(this,"身份证格式不正确！");
            return;
        }
        if(mail.length()>0
                &&!RegexUtils.isEmail(mail)){
            CommonUtil.showToast(this,"邮箱格式不正确！");
            return;
        }
        RegisterVo vo=new RegisterVo();

        vo.setBirth(birth);
        vo.setName(name);
        vo.setSex(sexId);
        vo.setPhone(phone);
        vo.setCard(card);
        vo.setVip(id);
        vo.setMail(mail);
        vo.setAddress(address);
        doRegister(vo);
    }

    private void doRegister(RegisterVo vo){
        CommonUtil.showLoadingView(this);
        registerRepository.reqRegister(vo, new
                RequestCallBack<BaseOutput>() {
                    @Override
                    public void onSuccess(BaseOutput object) {
                        CommonUtil.dismissLoadingView(thisActivity);
                        CommonUtil.showToast(thisActivity,"注册成功");
                    }

                    @Override
                    public void onFalse(String msg) {
                        CommonUtil.showToast(thisActivity,msg);
                    }
                });
    }

    private void actFinish(){
        this.finish();
        overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
    }
}
